package org.ThreadDemo.lock;

import java.util.concurrent.locks.ReentrantLock;

class LTicket{
    private int number= 10;
    private final ReentrantLock lock = new ReentrantLock(false);



    public void sale() {


        try{
            lock.lock();
            if(number>0){
                number--;
                System.out.println(Thread.currentThread().getName()+": 卖出："+"1票" +"剩下："+number);
            }
        }finally{
            lock.unlock();
        }

    }
}
public class LSaleTicket {



    public static void main(String[] args) {
        //Thread a = new Thread();
        LTicket ticket = new LTicket();
        new Thread(
                ()->{
                    for(int i=0;i<40;i++){
                        ticket.sale();
                    }
                },"AA"
        ).start();


        new Thread(
                ()->{
                    for(int i=0;i<40;i++){
                        ticket.sale();
                    }
                },"BB"
        ).start();

        new Thread(
                ()->{
                    for(int i=0;i<40;i++){
                        ticket.sale();
                    }
                },"CC"
        ).start();
    }

}
